#!/usr/bin/python

# Allow for the script to be run from the trunk/examples directory of the repo.
import sys
sys.path.append('..')

import sbak
import sbak.widget
from sbak.widget import Button, LayoutWindow, Container, LayoutDesktop

class MyApp(sbak.app.App):
    
    def __init__(self):
        
        # The usual initiallization.
        sbak.init()
        sbak.widget.init()
        sbak.app.App.__init__(self)
        
        # Create a layout-controlled interface.
        self.desktop = LayoutDesktop(
            resizable = True, align = 1, expand = True, fit = True,
            widgets = (
                
                # Top-aligned toolbar.
                LayoutWindow(
                    id = "ToolBar", region = (0,0,32,32), align = 2, fit=True,
                    
                    widgets = (
                        
                        # Exit button
                        Button(
                            region = (0,0,48,32), text = "Exit",
                            action = self.finish
                        ),
                        
                        # Duplicate button
                        Button(
                            region = (0,0,100,32), text = "Duplicate",
                            action = self.duplicate
                        )
                    )
                ), # End ToolBar
                
                # Empty "work area"
                Container()
            )
        )
        
        # The toolbar needs an absolute expand.
        self.desktop.set_widget_layout("ToolBar", rel_expand = -1)

        # Append the desktop to the app so it can receive events.
        self.append(self.desktop)
        
        # Apply the desktop's layout.
        # NOTE: This must be done after the desktop is appended to the app so
        # the desktop can post screen-updating events.
        self.desktop.apply_layout()
    
    def duplicate(self, e):
        """ Opens another instance of the current script as a new process. """
        import subprocess
        p = subprocess.Popen(sys.argv[0])

if __name__ == '__main__':
    myapp = MyApp()
    myapp.start()